
/*
 * Copyright (c) 2001 Sun Microsystems, Inc.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *       Sun Microsystems, Inc. for Project JXTA."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Sun", "Sun Microsystems, Inc.", "RosettaChat", "JXTA" and
 *    "Project JXTA" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact Project JXTA at http://www.jxta.org.
 *
 * 5. Products derived from this software may not be called "JXTA",
 *    nor may "JXTA" appear in their name, without prior written
 *    permission of Sun.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL SUN MICROSYSTEMS OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of Project JXTA.  For more
 * information on Project JXTA, please see
 * <http://www.jxta.org/>.
 *
 * This license is based on the BSD license adopted by the Apache Foundation.
 */

package net.jxta.myjxta.misc.rosettachat.translation;

import net.jxta.myjxta.misc.rosettachat.http.*;
import net.jxta.myjxta.misc.rosettachat.misc.EmptyIterator;
import java.net.*;
import java.io.*;
import java.util.*;

/**
 * Translator.java
 *
 * @author James Todd [jwtodd@pacbell.net]
 * @version
 */

public abstract class AbstractTranslator
	implements Translator {

    private static final String ENCODING_UTF8 = "UTF-8";
    private static List<String> baseLocales = null;
    private List<String> locales = null;
    private String sourceLocale = null;
    private String targetLocale = null;
    private URL targetURL = null;
    private String text = null;
    private URL url = null;
	private Map<String, String> filters = null;
	private Map<String, String> masks = null;

	static {
		baseLocales = new ArrayList<String>();

		baseLocales.add(ENGLISH);
		baseLocales.add(FRENCH);
		baseLocales.add(GERMAN);
		baseLocales.add(ITALIAN);
		baseLocales.add(JAPANESE);
		baseLocales.add(PORTUGUESE);
		baseLocales.add(SPANISH);
		baseLocales.add(SIMPLIFIED_CHINESE);
		baseLocales.add(TRADITIONAL_CHINESE);
	}

    public static Iterator getBaseLocales() {
		return (baseLocales != null ?
				baseLocales.iterator() : new EmptyIterator());
    }

    public AbstractTranslator() {
		this.sourceLocale = ENGLISH;

		addMask("<", ">");
    }

    public URL getTargetURL() {
		return this.targetURL;
    }

    public void setTargetURL(URL targetURL) {
		this.targetURL = targetURL;
    }

    public Iterator getLocales() {
		return ((this.locales != null) ?
				this.locales.iterator() : new EmptyIterator());
    }

    public String getSourceLocale() {
		return this.sourceLocale;
    }

    public void setSourceLocale(String sourceLocale)
		throws IllegalArgumentException {
		if (validLocale(sourceLocale)) {
			this.sourceLocale = sourceLocale;
		} else {
			throw new IllegalArgumentException("bad locale");
		}
    }

    public String getTargetLocale() {
		return this.targetLocale;
    }

    public void setTargetLocale(String targetLocale)
		throws IllegalArgumentException {
		if (validLocale(targetLocale)) {
			this.targetLocale = targetLocale;
		} else {
			throw new IllegalArgumentException("bad locale");
		}
    }

    public String getText() {
		return this.text;
    }

    public void setText(String text) {
		this.text = text;
    }

    public String translate()
		throws IllegalArgumentException, IllegalStateException,
			   IOException {
		return translate(null);
    }

    public String translate(String text)
		throws IllegalArgumentException, IllegalStateException,
			   IOException {
		return translate(text, null);
    }

    public abstract String translate(String text, String targetLocale)
		throws IllegalArgumentException, IllegalStateException,
			   IOException;

    protected void addLocale(String locale) {
		if (this.locales == null) {
			this.locales = new ArrayList<String>();
		}

		this.locales.add(locale);
    }

    protected abstract URL getURL();

    protected String getBody() {
		return null;
    }

	protected void addFilter(String pre, String post) {
		if (this.filters == null) {
			this.filters = new HashMap<String, String>();
		}

		this.filters.put(pre, post);
	}

	protected void addMask(String key, String value) {
		if (this.masks == null) {
			this.masks = new HashMap<String, String>();
		}

		this.masks.put(key, value);
	}

	protected Message dispatch(String text, String targetLocale)
		throws IllegalArgumentException, IllegalStateException,
			   IOException {
		preDispatch(text, targetLocale);

		return ((getBody() == null) ? getMessage() : postMessage());
	}

    protected void preDispatch(String text, String targetLocale)
		throws IllegalArgumentException, IllegalStateException {
		try {
			this.url = getURL();
		} catch (Exception e) {
			// munch
		}

		if (text != null) {
			setText(text);
		}

		if (targetLocale != null) {
			setTargetLocale(targetLocale);
		}

		if (this.url == null ||
			this.locales == null ||
			this.sourceLocale == null ||
			this.targetLocale == null ||
			(this.text == null &&
			 this.targetURL == null) ||
			! this.locales.contains(this.sourceLocale) ||
			! this.locales.contains(this.targetLocale) ||
			(this.sourceLocale != null &&
			 this.sourceLocale.equals(this.targetLocale)) ||
			(this.targetLocale != null &&
			 this.targetLocale.equals(this.sourceLocale))) {
			throw new IllegalStateException("bad state");
		}
    }

	protected String filter(String data) {

		Iterator filters = ((this.filters != null) ?
							this.filters.keySet().iterator() :
							new EmptyIterator());
		StringBuffer sb = new StringBuffer();

		while (filters.hasNext()) {
			String preDelimiter = (String)filters.next();
			String postDelimiter = this.filters.get(preDelimiter);
			String s = new String(data);
			int preIndex = -1;
			int postIndex = -1;
			int preDelimiterLength = preDelimiter.length();

			while ((preIndex = s.indexOf(preDelimiter)) > -1 &&
				   (postIndex = s.indexOf(postDelimiter,
										  preIndex +
										  preDelimiterLength)) > -1 &&
				   preIndex < postIndex) {
				sb.append(s = s.substring(preIndex + preDelimiterLength,
										  postIndex));
			}
		}

		if (sb.length() == 0) {
			sb = new StringBuffer(data.trim());
		}

		int l = sb.length();

		for (int i = 0; i < l; i++) {
			if (sb.charAt(i) == '\n') {
				sb.setCharAt(i, ' ');
			}
		}

		sb = new StringBuffer(mask(sb.toString()).trim());

		// Make the first character uppercase.
		if (sb.length() > 0) {
			char c = sb.charAt(0);
			if (! Character.isUpperCase(c)) {
				sb.setCharAt(0, Character.toUpperCase(c));
			}
		}

		return sb.toString();
    }

    protected String encode(String s) {
        try {
            s = URLEncoder.encode(s, ENCODING_UTF8);
        } catch (UnsupportedEncodingException uee) {
        }
        
        return s;
    }
    
    private boolean validLocale(String locale) {
		return (this.locales != null &&
				this.locales.contains(locale));
    }

    private Message getMessage()
		throws IOException {
		GetMessage getMessage = new GetMessage(this.url);

		getMessage.setUnicodeEncoding(true);
		getMessage.setFollowRedirect(true);

		return getMessage.dispatch();
    }

    private Message postMessage()
		throws IOException {
		PostMessage postMessage = new PostMessage(this.url);

		postMessage.setUnicodeEncoding(true);
		postMessage.setFollowRedirect(true);

//        postMessage.setHeader("Content-Type", "text/html; charset=UTF-8");
        postMessage.setHeader("Content-Type",
                              "application/x-www-form-urlencoded");
        postMessage.setHeader("Accept",
                              "image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,image/png,*/*");
        postMessage.setHeader("User-Agent",
                              "Mozilla/4.76C-CCK-MCD Netscape [en] (X11; U; SunOS 5.8 sun4u)");
        postMessage.setHeader("Accept-Language", "en");
        postMessage.setHeader("Accept-Encoding", "gzip");
        postMessage.setHeader("Accept-Charset", "iso-8859-1,*,utf-8");

		postMessage.setBody(getBody());

		return postMessage.dispatch();
    }

	private String mask(String data) {
		String s = new String(data);
		Iterator masks = ((this.masks != null) ?
						  this.masks.keySet().iterator() :
						  new EmptyIterator());

		while (masks.hasNext()) {
			String preDelimiter = (String)masks.next();
			String postDelimiter = this.masks.get(preDelimiter);
			int i = -1;
			int j = -1;
			int k = preDelimiter.length();
			int l = postDelimiter.length();

			while ((i = s.indexOf(preDelimiter)) > -1 &&
				   (j = s.indexOf(postDelimiter, i + k)) > -1) {
				s = new StringBuffer(s).delete(i, j + l).toString();
			}
		}

		return s;
	}
}
